package com.syni.mdd.sjb.common.datatreaure.utils;

import com.alibaba.fastjson.JSON;
import com.syni.mdd.sjb.common.datatreaure.entity.bo.dataBankBusiness.CdpAllBo;
import com.syni.mdd.sjb.common.datatreaure.entity.bo.dataBankBusiness.CdpClientBo;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

@Component
@Log4j2
public class CDPSimDataUtils {

    private static String resourcePath;

    private static String clientPath;

    @Value("${cdp.simdata.utils}")
    public void setResourcePath(String resourcePath) {
        CDPSimDataUtils.resourcePath = resourcePath;
    }

    @Value("${cdp.client.utils}")
    public void setClientPath(String clientPath) {
        CDPSimDataUtils.clientPath = clientPath;
    }

    public static CdpAllBo cdpAllBo;

    public static CdpClientBo cdpClientBo;

    public static CdpAllBo getCdpAllBo(){
        initCdpAllBo();
        return cdpAllBo;
    }

    public static CdpClientBo getCdpClientBo(){
        initCdpClientBo();
        return cdpClientBo;
    }

    public static void initCdpAllBo(){
        String jsonCode = null;
        try {
            jsonCode = IOUtils.toString(new InputStreamReader(new FileInputStream(new File(resourcePath)),"UTF-8"));
            cdpAllBo = JSON.parseObject(jsonCode, CdpAllBo.class);
        } catch (IOException e) {
            log.error(e.getMessage(),e);
        }
    }

    public static void initCdpClientBo(){
        String jsonCode = null;
        try {
            jsonCode = IOUtils.toString(new InputStreamReader(new FileInputStream(new File(clientPath)),"UTF-8"));
            cdpClientBo = JSON.parseObject(jsonCode, CdpClientBo.class);
        } catch (IOException e) {
            log.error(e.getMessage(),e);
        }
    }
}
